<template>
  <div class="layout-container">
    <!-- 侧边栏 -->
    <div class="layout-sidebar">
      <div class="sidebar-header">
        <h2 class="sidebar-title">校园外卖</h2>
      </div>
      
      <el-menu
        :default-active="$route.path"
        class="sidebar-menu"
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409eff"
        router
      >
        <el-menu-item index="/student">
          <el-icon><House /></el-icon>
          <span>首页</span>
        </el-menu-item>
        
        <el-menu-item index="/student/orders">
          <el-icon><Document /></el-icon>
          <span>我的订单</span>
        </el-menu-item>
        
        <el-menu-item index="/student/profile">
          <el-icon><User /></el-icon>
          <span>个人中心</span>
        </el-menu-item>
        
        <el-menu-item index="/student/address">
          <el-icon><Location /></el-icon>
          <span>收货地址</span>
        </el-menu-item>
      </el-menu>
    </div>
    
    <!-- 主内容区 -->
    <div class="layout-main">
      <!-- 顶部导航 -->
      <div class="layout-header">
        <div class="header-left">
          <h3 class="page-title">{{ getPageTitle() }}</h3>
        </div>
        
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="user-info">
              <el-avatar :size="32" :src="userStore.userInfo?.avatar">
                {{ userStore.userInfo?.username?.charAt(0)?.toUpperCase() }}
              </el-avatar>
              <span class="username">{{ userStore.userInfo?.username }}</span>
              <el-icon><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      
      <!-- 内容区域 -->
      <div class="layout-content">
        <router-view />
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '../stores/user'
import { ElMessageBox } from 'element-plus'
import { House, Document, User, Location, ArrowDown } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

const getPageTitle = () => {
  const routeTitles = {
    '/student': '首页',
    '/student/orders': '我的订单',
    '/student/profile': '个人中心',
    '/student/address': '收货地址'
  }
  return routeTitles[route.path] || '首页'
}

const handleCommand = async (command) => {
  if (command === 'profile') {
    router.push('/student/profile')
  } else if (command === 'logout') {
    try {
      await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      userStore.logout()
      router.push('/login')
    } catch {
      // 用户取消
    }
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
  width: 100%;
  display: flex;
  overflow: hidden;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.layout-sidebar {
  width: 240px;
  background: #304156;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
  z-index: 1000;
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid #1f2d3d;
  background: #2b3a4b;
}

.sidebar-title {
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  margin: 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.sidebar-menu {
  flex: 1;
  border: none;
}

.sidebar-menu .el-menu-item {
  height: 50px;
  line-height: 50px;
  margin: 4px 0;
  border-radius: 0 25px 25px 0;
  margin-right: 16px;
  transition: all 0.3s ease;
}

.sidebar-menu .el-menu-item:hover {
  background: #263445 !important;
  color: #409eff !important;
}

.sidebar-menu .el-menu-item.is-active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  color: #fff !important;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

.sidebar-menu .el-menu-item .el-icon {
  margin-right: 12px;
  font-size: 16px;
}

.layout-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #f0f2f5;
  width: 100%;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  position: relative;
}

.layout-header {
  height: 60px;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  z-index: 999;
  width: 100%;
  margin: 0;
  box-sizing: border-box;
  position: relative;
}

.header-left .page-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.user-info:hover {
  background: #f5f7fa;
}

.user-info .username {
  margin: 0 8px;
  font-size: 14px;
  color: #606266;
  font-weight: 500;
}

.user-info .el-icon {
  color: #909399;
  font-size: 12px;
}

.layout-content {
  flex: 1;
  overflow-y: auto;
  padding: 0;
  width: 100%;
  margin: 0;
  box-sizing: border-box;
  position: relative;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .layout-sidebar {
    width: 200px;
  }
  
  .sidebar-title {
    font-size: 16px;
  }
  
  .layout-header {
    padding: 0 16px;
  }
}

@media (min-width: 1600px) {
  .layout-sidebar {
    width: 280px;
  }
  
  .sidebar-title {
    font-size: 20px;
  }
  
  .layout-header {
    padding: 0 32px;
  }
}

/* 滚动条样式 */
.layout-content::-webkit-scrollbar {
  width: 6px;
}

.layout-content::-webkit-scrollbar-track {
  background: #f1f1f1;
}

.layout-content::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.layout-content::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

/* 动画效果 */
.sidebar-menu .el-menu-item {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.user-info {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
</style>
